package name.marz.to.go.ahg.core.executer;

import java.io.File;
import java.io.IOException;
import java.net.URI;
import java.util.logging.Logger;

import name.marz.to.go.ahg.core.AutoGetterHttpClient;
import name.marz.to.go.ahg.core.responsehandler.FileResponseHandler;

import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.methods.HttpGet;

/**
 * ファイルのダウンロードを行うExecuterです
 * 
 * @author mgoto
 *
 */
public class FileDownloadExecuter extends AbstractExecuter<File> {
	private static final Logger log = Logger.getLogger(FileDownloadExecuter.class.getName());
	
	protected File file;

	/**
	 * @param uri
	 * @param client
	 * @param file
	 */
	public FileDownloadExecuter(URI uri, AutoGetterHttpClient client, File file) {
		super(uri,client);
		this.file = file;
	}

	@Override
	public File execute() throws ClientProtocolException,IOException {
		// リクエスト生成
		HttpGet req = new HttpGet(uri);
		// レスポンスハンドラ生成
		FileResponseHandler handler = new FileResponseHandler(file,charset);
		//　リクエスト送信
		log.info("ダウンロード中 " + req.getURI());
		return client.execute(req, handler);
	}
}
